// Town 4: Arena Entrance
// t4Arena.txt
// This is the town script for the Arena Entrance in the HLPM.
//
// The Arena is a fun little addition. The player can test the party against
// opponents of any level. The INIT_STATE determines the party's level and
// stores it in flag (4,0). Then the player can sign up for a combat in one of 
// two ways: by talking to the Arena Master, or by signing up directly. The 
// Arena Master uses dialog nodes 1-10, and she can only assign combats that are 
// reasonably close to the party's level. For other combats, you have to sign up  
// manually. State 10 (which calls the function state 11) does this. State 12 
// governs the Healing Rune, for before or after combats.
//
// States 13-15 are for stairs.
//
// Flags used:
// 4 0 = party level
// 4 1 = level of requested combat
// 4 2 = 1 requested combat is ranged (otherwise melee)

begintownscript;

variables;
// short blah
short i,j,k,l,choice,dummy,won,which_state;
short what_num = 0;
short just_selected = 0;
string dlgstr;

body;

beginstate INIT_STATE;
	// Set names and dialogue pics. Also crime tolerance.
	set_name(23,"Arena Master");
	
	set_crime_tolerance(1);
	
	// Get rid of those ugly braziers. They're only there for light.
	set_terrain(19,22,0);
	set_terrain(19,11,0);
	
	// Determine the party's level, save it in flag (4,0) and print it.
	i = 0;
	if (char_ok(0))
		i = i + get_level(0);
	if (char_ok(1))
		i = i + get_level(1);
	if (char_ok(2))
		i = i + get_level(2);
	if (char_ok(3))
		i = i + get_level(3);
	i = i / party_size();
	set_flag(4,0,i);
	clear_buffer();
	append_string("The HLPM reads your party as level ");
	append_number(get_flag(4,0));
	get_buffer_text(dlgstr);
	print_str_color(dlgstr,3);

break;

beginstate EXIT_STATE;
break;

beginstate START_STATE;

// If the party is in combat mode, tell them to cut it out.
if (is_combat())
	message_dialog("Entering combat mode outside the Arena's combat area is NOT recommended.","");

// Punish crime with instant death.
if (get_crime_level() >= 1) {
	kill_char(1000,2,0);
	message_dialog("Crimes in the HLPM are punishable by instant death, as you discover the hard way.","");
	}

break;

beginstate 10;
	// If returning from the numeric input function, skip all these steps.
	if (just_selected == 0) {
		// If the party is already signed up for a combat, give them a choice to
		// back out.
		reset_dialog(); 
		if (get_flag(4,1) > 0) {
			add_dialog_str(0,"You are signed up for a fight. Do you wish to cancel?",0);
			add_dialog_choice(0,"Leave and stay on the sign-up sheet.");
			if (party_size() > 1)
				add_dialog_choice(1,"Erase your names.");
			else
				add_dialog_choice(1,"Erase your name.");
			if (run_dialog(1) == 2) {
				set_flag(4,1,0);
				set_flag(4,2,0);
				}
			else
				end();
			}
	
		// Give the party a choice to sign up for a fight. If they don't want
		// to, end the state.
		add_dialog_str(0,"On the piece of paper in front of you, there are many numbers, starting at 1, then going to 5 and incrementing by 5's until they reach 100. In between each of these numbers are four lines.",0);
		if (party_size() > 1)
			add_dialog_str(1,"Evidently you are supposed to write your names on the appropriate lines if you wish to fight at that difficulty level.",0);
		else
			add_dialog_str(1,"Evidently you are supposed to write your name on the appropriate line if you wish to fight at that difficulty level.",0);
		add_dialog_str(2,"What do you do?",0);
		add_dialog_choice(0,"Leave.");
		add_dialog_choice(1,"Find a specific line and sign up for a combat.");
		if (run_dialog(1) == 1)
			end();
	
		// Get numeric input.
		message_dialog("In the following text box, enter a numeral that is a multiple of 5 and is in the range 5-100. Examples include _10,_ _25,_ and _90._","");
		which_state = 10;
		set_state_continue(11); // Call numeric input function
		}
		
	// Re-zero the variable after returning from the function.
	just_selected = 0;
	
	// If they didn't choose an integral multiple of 5, send them back to the
	// numeric input function.
	if (what_num % 5 != 0) {
		reset_dialog();
		add_dialog_str(0,"Oops! That wasn't an integer multiple of 5. Again, sample responses include _5,_ _30,_ and _55_ (written without quotes, obviously).",0);
		add_dialog_str(1,"What do you want to do?",0);
		add_dialog_choice(0,"Try again.");
		add_dialog_choice(1,"Cancel all this.");
		if (run_dialog(1) == 1)
			set_state_continue(11);
		else
			end();
		}
	
	// Tell them what level they have selected.
	set_flag(4,1,what_num);
	clear_buffer();
	append_string("You sign up for a level ");
	append_number(get_flag(4,1));
	append_string(" combat.");
	get_buffer_text(dlgstr);
	message_dialog(dlgstr,"");
	
	// Ask if they want to fight a ranged combat. If so, set a flag.
	reset_dialog();
	add_dialog_str(0,"There is a space to mark down _ranged._ Would you like to make your fight a ranged (missile-based) combat?",0);
	add_dialog_choice(0,"No. I want to fight a regular melee combat.");
	add_dialog_choice(1,"Yes. Put them on the other side of a pit from me!");
	if (run_dialog(1) == 2) {
		set_flag(4,2,1);
		message_dialog("You sign up for a ranged combat.","");
		}
break;

beginstate 11;
	// Numeric input function. Sets variable what_num to whatever the input was.
	// Then goes to which_state, which was presumably defined in the state that
	// called this function.
	//
	// NOTE: This function uses the variable just_selected to tell whether BoA
	// is returning from the function or hasn't started it yet. Make sure
	// just_selected is 0 when you begin the state that calls this function, and
	// make sure to zero it again after this function is over.
	
	what_num = 0;
	
	get_text_response("What level?");
	check_text_response_match("1");
	if (got_text_match())
		what_num = 1;
	check_text_response_match("2");
	if (got_text_match())
		what_num = 2;
	check_text_response_match("3");
	if (got_text_match())
		what_num = 3;
	check_text_response_match("4");
	if (got_text_match())
		what_num = 4;
	check_text_response_match("5");
	if (got_text_match())
		what_num = 5;
	check_text_response_match("6");
	if (got_text_match())
		what_num = 6;
	check_text_response_match("7");
	if (got_text_match())
		what_num = 7;
	check_text_response_match("8");
	if (got_text_match())
		what_num = 8;
	check_text_response_match("9");
	if (got_text_match())
		what_num = 9;
	check_text_response_match("10");
	if (got_text_match())
		what_num = 10;
	check_text_response_match("11");
	if (got_text_match())
		what_num = 11;
	check_text_response_match("12");
	if (got_text_match())
		what_num = 12;
	check_text_response_match("13");
	if (got_text_match())
		what_num = 13;
	check_text_response_match("14");
	if (got_text_match())
		what_num = 14;
	check_text_response_match("15");
	if (got_text_match())
		what_num = 15;
	check_text_response_match("16");
	if (got_text_match())
		what_num = 16;
	check_text_response_match("17");
	if (got_text_match())
		what_num = 17;
	check_text_response_match("18");
	if (got_text_match())
		what_num = 18;
	check_text_response_match("19");
	if (got_text_match())
		what_num = 19;
	check_text_response_match("20");
	if (got_text_match())
		what_num = 20;
	check_text_response_match("21");
	if (got_text_match())
		what_num = 21;
	check_text_response_match("22");
	if (got_text_match())
		what_num = 22;
	check_text_response_match("23");
	if (got_text_match())
		what_num = 23;
	check_text_response_match("24");
	if (got_text_match())
		what_num = 24;
	check_text_response_match("25");
	if (got_text_match())
		what_num = 25;
	check_text_response_match("26");
	if (got_text_match())
		what_num = 26;
	check_text_response_match("27");
	if (got_text_match())
		what_num = 27;
	check_text_response_match("28");
	if (got_text_match())
		what_num = 28;
	check_text_response_match("29");
	if (got_text_match())
		what_num = 29;
	check_text_response_match("30");
	if (got_text_match())
		what_num = 30;
	check_text_response_match("31");
	if (got_text_match())
		what_num = 31;
	check_text_response_match("32");
	if (got_text_match())
		what_num = 32;
	check_text_response_match("33");
	if (got_text_match())
		what_num = 33;
	check_text_response_match("34");
	if (got_text_match())
		what_num = 34;
	check_text_response_match("35");
	if (got_text_match())
		what_num = 35;
	check_text_response_match("36");
	if (got_text_match())
		what_num = 36;
	check_text_response_match("37");
	if (got_text_match())
		what_num = 37;
	check_text_response_match("38");
	if (got_text_match())
		what_num = 38;
	check_text_response_match("39");
	if (got_text_match())
		what_num = 39;
	check_text_response_match("40");
	if (got_text_match())
		what_num = 40;
	check_text_response_match("41");
	if (got_text_match())
		what_num = 41;
	check_text_response_match("42");
	if (got_text_match())
		what_num = 42;
	check_text_response_match("43");
	if (got_text_match())
		what_num = 43;
	check_text_response_match("44");
	if (got_text_match())
		what_num = 44;
	check_text_response_match("45");
	if (got_text_match())
		what_num = 45;
	check_text_response_match("46");
	if (got_text_match())
		what_num = 46;
	check_text_response_match("47");
	if (got_text_match())
		what_num = 47;
	check_text_response_match("48");
	if (got_text_match())
		what_num = 48;
	check_text_response_match("49");
	if (got_text_match())
		what_num = 49;
	check_text_response_match("50");
	if (got_text_match())
		what_num = 50;
	check_text_response_match("51");
	if (got_text_match())
		what_num = 51;
	check_text_response_match("52");
	if (got_text_match())
		what_num = 52;
	check_text_response_match("53");
	if (got_text_match())
		what_num = 53;
	check_text_response_match("54");
	if (got_text_match())
		what_num = 54;
	check_text_response_match("55");
	if (got_text_match())
		what_num = 55;
	check_text_response_match("56");
	if (got_text_match())
		what_num = 56;
	check_text_response_match("57");
	if (got_text_match())
		what_num = 57;
	check_text_response_match("58");
	if (got_text_match())
		what_num = 58;
	check_text_response_match("59");
	if (got_text_match())
		what_num = 59;
	check_text_response_match("60");
	if (got_text_match())
		what_num = 60;
	check_text_response_match("61");
	if (got_text_match())
		what_num = 61;
	check_text_response_match("62");
	if (got_text_match())
		what_num = 62;
	check_text_response_match("63");
	if (got_text_match())
		what_num = 63;
	check_text_response_match("64");
	if (got_text_match())
		what_num = 64;
	check_text_response_match("65");
	if (got_text_match())
		what_num = 65;
	check_text_response_match("66");
	if (got_text_match())
		what_num = 66;
	check_text_response_match("67");
	if (got_text_match())
		what_num = 67;
	check_text_response_match("68");
	if (got_text_match())
		what_num = 68;
	check_text_response_match("69");
	if (got_text_match())
		what_num = 69;
	check_text_response_match("70");
	if (got_text_match())
		what_num = 70;
	check_text_response_match("71");
	if (got_text_match())
		what_num = 71;
	check_text_response_match("72");
	if (got_text_match())
		what_num = 72;
	check_text_response_match("73");
	if (got_text_match())
		what_num = 73;
	check_text_response_match("74");
	if (got_text_match())
		what_num = 74;
	check_text_response_match("75");
	if (got_text_match())
		what_num = 75;
	check_text_response_match("76");
	if (got_text_match())
		what_num = 76;
	check_text_response_match("77");
	if (got_text_match())
		what_num = 77;
	check_text_response_match("78");
	if (got_text_match())
		what_num = 78;
	check_text_response_match("79");
	if (got_text_match())
		what_num = 79;
	check_text_response_match("80");
	if (got_text_match())
		what_num = 80;
	check_text_response_match("81");
	if (got_text_match())
		what_num = 81;
	check_text_response_match("82");
	if (got_text_match())
		what_num = 82;
	check_text_response_match("83");
	if (got_text_match())
		what_num = 83;
	check_text_response_match("84");
	if (got_text_match())
		what_num = 84;
	check_text_response_match("85");
	if (got_text_match())
		what_num = 85;
	check_text_response_match("86");
	if (got_text_match())
		what_num = 86;
	check_text_response_match("87");
	if (got_text_match())
		what_num = 87;
	check_text_response_match("88");
	if (got_text_match())
		what_num = 88;
	check_text_response_match("89");
	if (got_text_match())
		what_num = 89;
	check_text_response_match("90");
	if (got_text_match())
		what_num = 90;
	check_text_response_match("91");
	if (got_text_match())
		what_num = 91;
	check_text_response_match("92");
	if (got_text_match())
		what_num = 92;
	check_text_response_match("93");
	if (got_text_match())
		what_num = 93;
	check_text_response_match("94");
	if (got_text_match())
		what_num = 94;
	check_text_response_match("95");
	if (got_text_match())
		what_num = 95;
	check_text_response_match("96");
	if (got_text_match())
		what_num = 96;
	check_text_response_match("97");
	if (got_text_match())
		what_num = 97;
	check_text_response_match("98");
	if (got_text_match())
		what_num = 98;
	check_text_response_match("99");
	if (got_text_match())
		what_num = 99;
	check_text_response_match("100");
	if (got_text_match())
		what_num = 100;
	check_text_response_match("god");
	if (got_text_match())
		what_num = 250;
		
	// Make sure the player actually entered something correct, and if not,
	// start over.
	if (what_num == 0) {
		message_dialog("Unfortunately, that was not an integer between 1 and 100. (Again, sample responses would include _15,_ _37,_ or _41._)","Let's try again.");
		set_state_continue(11);
		}
		
	just_selected = 1;
	set_state_continue(which_state);
break;

beginstate 12;
	// Healing rune. Restores health and spell power. Also sets the day forward
	// by one, which resets special abilities.
	reset_dialog();
	add_dialog_str(0,"The rune here says, _Healing Rune._ What do you do?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Step on it.");
	if (run_dialog(1) == 1)
		block_entry(1);
	else {
		revive_party();
		set_ticks_forward(5000);
		message_dialog("A soothing feeling washes over you. You are restored!","");
		}
break;

beginstate 13; // Stairs up to Arena combat room
	reset_dialog();
	add_dialog_str(0,"These stairs lead to the Arena combat room. What would you like to do?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Climb the stairs.");
	if (run_dialog(1) == 1)
		block_entry(1);
	else
		move_to_new_town(5,42,19);
break;

beginstate 14; // Stairs up to Shopping Area
	reset_dialog();
	add_dialog_str(0,"These stairs lead to the Shopping Area. What would you like to do?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Climb the stairs.");
	if (run_dialog(1) == 1)
		block_entry(1);
	else
		move_to_new_town(3,23,42);
break;

beginstate 15; // Stairs down to Entrance
	reset_dialog();
	add_dialog_str(0,"These stairs lead to the Entrance. What would you like to do?",0);
	add_dialog_choice(0,"Leave.");
	add_dialog_choice(1,"Descend the stairs.");
	if (run_dialog(1) == 1)
		block_entry(1);
	else
		move_to_new_town(0,22,17);
break;